package com.neusoft.neuiotms.dcom.converter.change;

import java.util.Date;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import com.neusoft.bizcore.web.dto.AbstractConverter;
import com.neusoft.neuiotms.dcom.dto.change.ChangeCiPropertyDTO;
import com.neusoft.neuiotms.dcom.model.change.ChangeCiProperty;
import com.neusoft.neuiotms.dcom.repository.change.ChangeCiPropertyRepository;

@Component
public class ChangeCiPropertyConverter extends AbstractConverter<ChangeCiProperty, ChangeCiPropertyDTO> {

    @Autowired
    private ChangeCiPropertyRepository changeCiPropertyRepository;

    @Override
    public ChangeCiProperty toModel(final ChangeCiPropertyDTO dto) {
        ChangeCiProperty model;
        if (dto.isNew()) {
            model = new ChangeCiProperty();
        } else {
            model = this.changeCiPropertyRepository.getOne(dto.getId());
        }
        model.setUpdateTime(new Date());
        model.setPropertyId(dto.getKey());
        model.setPropertyName(dto.getName());
        model.setBeforeChange(dto.getValue());
        model.setAfterChange(dto.getAfterChange());
        model.setTaskId(dto.getTaskId());
        return model;
    }

    @Override
    public ChangeCiPropertyDTO toDTO(final ChangeCiProperty model, final boolean forListView) {
        final ChangeCiPropertyDTO dto = new ChangeCiPropertyDTO();
        dto.setId(model.getId());
        dto.setKey(model.getPropertyId());
        dto.setName(model.getPropertyName());
        dto.setValue(model.getBeforeChange());
        dto.setAfterChange(model.getAfterChange());
        dto.setTaskId(model.getTaskId());
        dto.setCreateTime(model.getCreateTime());
        dto.setUpdateTime(model.getUpdateTime());

        return dto;
    }
}
